package com.home.qh.webapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.home.qh.model.Booking;
import com.home.qh.model.Customer;
import com.home.qh.model.Partner;
import com.home.qh.model.Port;
import com.home.qh.model.Voyage;
import com.home.qh.service.BookingManager;
import com.home.qh.service.CustomerManager;
import com.home.qh.service.PartnerManager;
import com.home.qh.service.PortManager;
import com.home.qh.service.VoyageManager;

@Controller
public class BookingController {
	private transient final Log log = LogFactory.getLog(BookingController.class);
	private PortManager portManager;
	private BookingManager bookingManager;
	private VoyageManager voyageManager;
	private CustomerManager customerManager;
	private PartnerManager partnerManager;

	@Autowired
	private ApplicationContext context;
	
	@Autowired
	public void setPortManager(PortManager portManager) {
		this.portManager = portManager;
	}
	
	@Autowired
	public void setBookingManager(BookingManager bookingManager) {
		this.bookingManager = bookingManager;
	}
	
	@Autowired
	public void setVoyageManager(VoyageManager voyageManager) {
		this.voyageManager = voyageManager;
	}
	
	@Autowired
	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}
	
	@Autowired
	public void setPartnerManager(PartnerManager partnerManager) {
		this.partnerManager = partnerManager;
	}

	@RequestMapping(value = "/booking/view_booking.*", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView listBookings(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("entering 'handleRequest' method...");
		}

		Booking bookingSearch = new Booking();
		String partnerValue = (String) request.getParameter("partner");
		
		//Shipper parameters
		String shipperIdStrs = (String)request.getParameter("shipperId");
		String shipperName = "";
		if((String)request.getParameter("shipperName") != null){
			shipperName = (String)request.getParameter("shipperName");
		}
		
		//Consignee parameters
		String consigneeIdStr = (String) request.getParameter("consigneeId");
		String consigneeName = "";
		if((String)request.getParameter("consigneeName")!=null){
			consigneeName = (String)request.getParameter("consigneeName");
		}
		
		//Voyage parameters
		String voyageIdStr = (String)request.getParameter("voyageId");
		String voyageName = "";
		if ((String)request.getParameter("voyageName")!=null)
			voyageName= (String)request.getParameter("voyageName");
		
		//PortLoading parameters
		String portLoadingIdStr = (String) request.getParameter("portLoadingId");
		String portLoadingName = "";
		if((String)request.getParameter("portLoadingName")!=null){
			portLoadingName = (String)request.getParameter("portLoadingName");
		}
		
		//portDischarge parameters
		String portDischargeIdStr = (String) request.getParameter("portDischargeId");
		String portDischargeName = "";
		if((String)request.getParameter("portDischargeName")!=null){
			portDischargeName = (String)request.getParameter("portDischargeName");
		}
		
		if (shipperIdStrs != null && !"".equalsIgnoreCase(shipperIdStrs.trim()) && !"null".equalsIgnoreCase(shipperIdStrs)){
			Long paramShipperId = null;
			try {
				paramShipperId = Long.parseLong(shipperIdStrs);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (paramShipperId != null) {
				Customer shipperSearch = new Customer();
				shipperSearch.setId(paramShipperId);
				bookingSearch.setShipper(shipperSearch);
			}else{
				bookingSearch.setShipper(null);
			}
		}
		
		if (consigneeIdStr != null && !"".equalsIgnoreCase(consigneeIdStr.trim()) && !"null".equalsIgnoreCase(consigneeIdStr)){
			Long paramConsigneeId = null;
			try {
				paramConsigneeId = Long.parseLong(consigneeIdStr);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (paramConsigneeId != null) {
				Customer consigneeSearch = new Customer();
				consigneeSearch.setId(paramConsigneeId);
				bookingSearch.setConsignee(consigneeSearch);
			}else{
				bookingSearch.setConsignee(null);
			}
		}
		
		if (partnerValue != null && !"".equalsIgnoreCase(partnerValue.trim()) && !"null".equalsIgnoreCase(partnerValue)){
			Long paramPartnerId = null;
			try {
				paramPartnerId = Long.parseLong(partnerValue);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (paramPartnerId != null) {
				Partner partnerSearch = new Partner();
				partnerSearch.setId(paramPartnerId);
				bookingSearch.setPartner(partnerSearch);
			}else{
				bookingSearch.setPartner(null);
			}
		}
		
		if (voyageIdStr != null && !"".equalsIgnoreCase(voyageIdStr.trim()) && !"null".equalsIgnoreCase(voyageIdStr)){
			Long paramVoyageId = null;
			try {
				paramVoyageId = Long.parseLong(voyageIdStr);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (paramVoyageId != null) {
				Voyage voyageSearch = new Voyage();
				voyageSearch.setId(paramVoyageId);
				bookingSearch.setVoyage(voyageSearch);
			}else{
				bookingSearch.setVoyage(null);
			}
		}
		
		if (portLoadingIdStr != null && !"".equalsIgnoreCase(portLoadingIdStr.trim()) && !"null".equalsIgnoreCase(portLoadingIdStr)){
			Long portLoadingId = null;
			try {
				portLoadingId = Long.parseLong(portLoadingIdStr);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (portLoadingId != null) {
				Port portSearch = new Port();
				portSearch.setId(portLoadingId);
				bookingSearch.setPortLoading(portSearch);
			}else{
				bookingSearch.setPortLoading(null);
			}
		}
		
		if (portDischargeIdStr != null && !"".equalsIgnoreCase(portDischargeIdStr.trim()) && !"null".equalsIgnoreCase(portDischargeIdStr)){
			Long portDischargeId = null;
			try {
				portDischargeId = Long.parseLong(portDischargeIdStr);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (portDischargeId != null) {
				Port portSearch = new Port();
				portSearch.setId(portDischargeId);
				bookingSearch.setPortDischarge(portSearch);
			}else{
				bookingSearch.setPortDischarge(null);
			}
		}

		ModelAndView view = new ModelAndView("booking/view_booking");
		view.addObject("voyageList", voyageManager.getAll());
		view.addObject("customerList", customerManager.getAll());
		view.addObject("partnerList", partnerManager.getAll());
		view.addObject("portList", portManager.getAll());
		view.addObject("voyageName", voyageName);
		view.addObject("voyageId", voyageIdStr);
		view.addObject("shipperName",shipperName);
		view.addObject("shipperId",shipperIdStrs);
		view.addObject("consigneeName", consigneeName);
		view.addObject("consigneeId", consigneeIdStr);
		view.addObject("portLoadingName", portLoadingName);
		view.addObject("portLoadingId", portLoadingIdStr);
		view.addObject("portDischargeName", portDischargeName);
		view.addObject("portDischargeId", portDischargeIdStr);
		
		
		view.addObject("bookingSearch", bookingSearch);

		view.addObject("bookingList",	bookingManager.getBookingByBooking(bookingSearch, null, false, -1, -1));
		return view;

	}

}
